#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QFile>
#include <QtSerialPort/QSerialPort> //串口类
#include <QtSerialPort/QSerialPortInfo> //串口信息类
#include <QTimer>
#include <QProcess>
#include <QDateTime>
#include <QGraphicsDropShadowEffect>//阴影绘制
#include <QStackedLayout>
#include <QSqlDatabase>
#include <QMediaPlayer> //播放器
#include <QVideoWidget> //视频显示窗口
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "gpio.h"
#include "palyer.h"
#include "syncworker.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE


class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    QSerialPort *serialPort;
    QTimer *timer;
    QTimer *reminderTimer;
    QByteArray serialBuffer; // 共享缓冲区
    QStackedLayout* m_stackedLayout;
    QGraphicsDropShadowEffect *shadowBackSet();
    QGraphicsDropShadowEffect *createShadowEffect();
    void keyPressEvent(QKeyEvent *event) override;
    void getTodoList();
    void checkKeys();
    void debounceCheck();     // 消抖检测函数
    void mergeAudioVideo();//合并音视频函数
    void showPageIcon(uint8_t nowPageIndex);  //按页显示缩略图
    // 获取某一行中有效数据的数量（非空字符串）
    int countValidDataInRow(int row) const;

    void updateDeviceStatus(const QString& field, const QString& value);
    void uploadVideo(const QString &filePath, const QString &serverIp = "47.109.75.129", int port = 3000);

signals:
    void newDataAvailable(); // 当有新数据时触发的信号
    void alarmSentSuccess();
    void alarmSentFailed(const QString &error);

private slots:
    //自定义一个槽函数
    void readData();
    void updateTimeDisplay();
    void checkReminders();
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_reminder1_clicked();

    void on_reminder2_clicked();

    void on_reminder3_clicked();

    void on_reminder4_clicked();

    void on_reminder5_clicked();

    void on_reminder6_clicked();

    void on_pushButton_3_clicked();

    void updateVideoIcon(const QString &videoPath,const QString &storagePath);

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    void on_pushButton_9_clicked();

    void on_pushButton_10_clicked();

    void on_pushButton_12_clicked();

    void on_pushButton_11_clicked();

    void on_prevPage_clicked();

    void on_nextPage_clicked();

    void on_pushButton_13_clicked();

    void uploadSensorData();
    void pollControlCommands();
    void handleUploadFinished(QNetworkReply *reply);
    void handlePollFinished(QNetworkReply *reply);
    void sendManualAlarm(const QString &message = "检测到紧急情况");
    void handleAlarmError(QNetworkReply* reply, const QList<QSslError> &errors);
    void handleAlarmResponse(QNetworkReply *reply);

    void on_pushButton_15_clicked();

private:
    Ui::Widget *ui;
    QFile file;
    QProcess* process;
    QDateTime m_customTime;
    int m_startPosX;
    int m_curPosX;
    QWidget *foucs;
    QSqlDatabase db;
    int todoListId[6] = {0};//代办的事项的ID
    quint8 carried[3] = {0};//携带的物品
    QTimer *keyTimer; // 定时器

    Gpio gpio;
    QTimer *debounceTimer;    // 消抖定时器（10ms）
    QList<uint8_t> pendingKeys; // 等待确认的按键

    QMediaPlayer *pPlayer;
    myPalyer *my_player;
    QString videoPath[6];
    QVector<QVector<QString>> videoTable = QVector<QVector<QString>>(4, QVector<QString>(6, ""));//总共4页的视频表
    QVector<QVector<QString>> thumbnailTable = QVector<QVector<QString>>(4, QVector<QString>(6, ""));//总共4页的缩略图表
    uint8_t storageIndex = 0;//存储到第几页index
    uint8_t pageIndex = 0;//当前在第几页index

    //QProcess *gstProcess;
    QProcess *videoProcess;
    QProcess *audioProcess;
    QString videoFile = "./videos/video.ts";
    QString audioFile = "./videos/audio.wav";

    QNetworkAccessManager *networkManager;
    QTimer *uploadTimer;
    QTimer *pollTimer;

    // 控制指令执行
    void setHumidifier(bool on);
    void setLight(bool on);

    SyncWorker *reminderSync;

};


#endif // WIDGET_H
